﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Styx.WoWInternals;

namespace ChatBuddy.Classes.Helpers.Friend_Helper
{
    class FriendLister
    {
        public int TotalBnetFriends, OoFriends;
        private List<FriendMember> _bNetFriends;
        private List<FriendMember> _oOFriends;

        private int BattleNetFriends()
        {
            return Lua.GetReturnVal<int>("local numBNetTotal, numBNetOnline = BNGetNumFriends(); return numBNetTotal", 0);
        }

        private static int OOFriends()
        {
            return Lua.GetReturnVal<int>("return GetNumFriends()", 0);
        }

        public List<FriendMember> GetBnetFriends()
        {
            try
            {
                _bNetFriends = new List<FriendMember>();
                _bNetFriends.Clear();

                var totalFriends = BattleNetFriends();

                if (totalFriends == 0)
                    return null;

                for (int i = 1; totalFriends != 0 && i <= totalFriends; ++i)
                {
                    var newFriend = new FriendMember
                    {
                        Name = Lua.GetReturnValues(String.Format("return BNGetFriendInfo({0})", i))[2]
                    };
                    _bNetFriends.Add(newFriend);
                }
                return _bNetFriends;
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
                return null;
            }
        }

        public List<FriendMember> GetOoFriendMembers()
        {
            try
            {
                _oOFriends = new List<FriendMember>();
                _oOFriends.Clear();

                var totalFriends = OOFriends();

                if (totalFriends == 0)
                    return null;

                for (var i = 1; totalFriends != 0 && i <= totalFriends; ++i)
                {
                    var newFriend = new FriendMember
                    {
                        Name = Lua.GetReturnVal<string>(String.Format("local name, level, klasse, ort, online = GetFriendInfo({0}); return name", i), 0)
                    };
                    _oOFriends.Add(newFriend);
                }
                return _oOFriends;
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
                return null;
            }
        }
    }
}
